package cn.edu.njust.epms.dao;

import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.entity.ATRecord;
import cn.edu.njust.epms.entity.ModApply;
import cn.edu.njust.epms.entity.User;
import cn.edu.njust.epms.entity.UserDetails;

import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface UserDao {
    @Insert("insert into user (uName,uPw,uSts,salt) values(#{uName},#{uPw},0,#{salt})")
    public boolean add(User user);
    @Select("select * from user where uName like #{uName}")
    public User selectByName(String uName);

//    @Select("SELECT * FROM user inner join udetails on udetails.uId=user.uId")
//    @Results({
////            @Result(column = "uId",property = "uId"),
////            @Result(column = "pid",property = "ud.pid"),
////            @Result(column = "uName",property = "uName"),
////            @Result(column = "name",property = "ud.name"),
////            @Result(column = "addr",property = "ud.addr"),
////            @Result(column = "phone",property = "ud.phone"),
////            @Result(column = "gender",property = "ud.gender"),
////            @Result(column = "isSelf",property = "ud.isSelf"),
////            @Result(column = "isQuartine",property = "ud.isQuartine")
//    })
//    public ArrayList<UserDetails> selectAll2();
    @Select("select * from user")
    public ArrayList<User> selectAll();
    @Select("select * from user where uId = #{uId}")
    public User selectByuId(Integer uId);
//    @Select("select * from udetails where uId = #{uId}")
//    public ArrayList<UserDetails> selectDetailByUser(User user);
@Select("<script>"+
        "SELECT * FROM user inner join udetails on udetails.uId=user.uId"+
        "<where>"+
        "   <if test='r.name != null and r.name.length > 0'>"+
        "       AND name like #{r.name}"+
        "   </if>"+
        "   <if test='r.uName != null and r.uName.length > 0'>"+
        "       AND uNname like #{r.uName}"+
        "   </if>"+
        "   <if test='r.uId != null'>"+
        "       AND udetails.uId = #{r.uId}"+
        "   </if>"+
        "   <if test='r.pid != null and r.pid.length > 0'>"+
        "       AND udetails.pid like #{r.pid}"+
        "   </if>"+
        "</where>"+
        "</script>")
//@Results({
//        @Result(column = "uId",property = "uId"),
//        @Result(column = "pid",property = "ud.pid"),
//        @Result(column = "uName",property = "uName"),
//        @Result(column = "name",property = "ud.name"),
//        @Result(column = "addr",property = "ud.addr"),
//        @Result(column = "phone",property = "ud.phone"),
//        @Result(column = "gender",property = "ud.gender"),
//        @Result(column = "isSelf",property = "ud.isSelf"),
//        @Result(column = "isQuartine",property = "ud.isQuartine")
//})
    public List<UserDetails> selectByCondition(@Param("r") R r);



    @Update("update user set uSts=#{uSts} where uId=#{uId}")
    public Boolean updateRole(User user);

    @Update("update user set uPw=#{pwd} where uId=#{uId}")
    public Boolean updatePwd(Integer uId,String pwd);

    @Update("update udetails set name=#{name},addr=#{addr},phone=#{phone},gender=#{gender} where pid=#{pid}")
    public Boolean updateInfo(UserDetails ud);

    @Insert("insert into mod_info (pid,name,addr,phone,gender,st,note,apptime,uid,isself) values (#{pid},#{name},#{addr},#{phone},#{gender},#{st},#{note},#{apptime},#{uid},#{isself})")
    public Boolean applyMod(ModApply ma);

    @Update("update mod_info set audtime=#{audtime},note=#{note},st=#{st} where mid=#{mid}")
    public Boolean updateAppMod(ModApply ma);

    @Select("<script>"+
            "SELECT * FROM mod_info"+
            "<where>"+
            "   <if test='r.name != null and r.name.length > 0'>"+
            "       AND name like #{r.name}"+
            "   </if>"+
            "   <if test='r.uId != null'>"+
            "       AND uId = #{r.uId}"+
            "   </if>"+
            "   <if test='r.st != null'>"+
            "       AND st = #{r.st}"+
            "   </if>"+
            "   <if test='r.pid != null and r.pid.length > 0'>"+
            "       AND pid like #{r.pid}"+
            "   </if>"+
            "   <if test='r.date1 != null and r.date2 != null'>"+
            "       AND apptime between #{r.date1} and #{r.date2}"+
            "   </if>"+
            "</where>"+
            "</script>")
    @Results({
            @Result(column = "mid",property = "mid"),
            @Result(column = "apptime",property = "apptime"),
            @Result(column = "st",property = "st"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "note",property = "note"),
            @Result(column = "name",property = "name"),
            @Result(column = "addr",property = "addr"),
            @Result(column = "gender",property = "gender"),
            @Result(column = "audtime",property = "audtime"),
            @Result(column = "phone",property = "phone")
    })
    public List<ModApply> selectAppByCondition(@Param("r") R r);

    @Select("select * from mod_info where mid=#{mid}")
    public ModApply selectModById(@Param("mid") Integer mid);

    @Insert("insert into udetails (uId, name, pid, addr, phone, gender, isSelf, isQuarantine) VALUES (#{uId},#{name},#{pid},#{addr},#{phone},#{gender},#{isSelf},#{isQuarantine})")
    public Boolean addInfo(UserDetails ud);

    @Delete("delete from user where uId=#{uId}")
    public Boolean deleteUser(User user);

    @Delete("delete from udetails where pid=#{pid}")
    public Boolean deleteuDetails(UserDetails ud);

    @Delete("delete from mod_info where mid=#{mid}")
    public Boolean deleteAppRecord(ModApply ma);

    @Select("select * from udetails where pid=#{pid}")
    public UserDetails selectUdByPid(@Param("pid") String pid);

    /**
     *
     * @param uId
     * @return
     */
    @Select("select * from udetails where uId=#{uId}")
    public List<UserDetails> selectUdByuId(@Param("uId") Integer uId);

    @Select("select * from udetails where uId=#{uId} and isSelf=1")
    public List<UserDetails> selectSelfByuId(@Param("uId") Integer uId);

//    @Select("select * from udetails where pid=#{pid}")
//    public UserDetails selectUdByPid(@Param("pid") String pid);
}
